//
//  JSTPictureViewController.m
//  KYKJPackDemo
//
//  Created by 01 on 17/5/16.
//  Copyright © 2017年 ???. All rights reserved.
//

#import "JSTPictureViewController.h"
#import "JSTPictureView.h"

@interface JSTPictureViewController ()<PictureViewDelegate, UIScrollViewDelegate,UIActionSheetDelegate>
{
    //总数
    NSInteger allCount;
    
    NSMutableArray *imgsArray;
    
    NSInteger pageIndex;
}
@property (nonatomic, strong) JSTPictureView *pictureView;

@property (nonatomic, assign) BOOL isHideBar;

@end

@implementation JSTPictureViewController

- (void)viewWillLayoutSubviews
{
    self.view.frame = kAppDelegate.window.bounds;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self createUI];
}

#pragma mark - createUI
-(void)createUI
{
    _pictureView = [[JSTPictureView alloc] initWithFrame:self.view.bounds andOwner:self];
    _pictureView.backgroundColor = [UIColor whiteColor];
    
    _pictureView.delegate = self;
    self.view = _pictureView;
}
#pragma mark - 赋值
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    _isHideBar = YES;
    if (_block) {
        _block(imgsArray);
    }
}

#pragma mark - 外部调用
- (void)setValueWithImages:(NSArray *)images andImagView:(NSInteger) tag
{
    UIScrollView *imageBack = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    imageBack.bounces = NO;
    self.navigationItem.title = [NSString stringWithFormat:@"%zd/%zd",tag,images.count];
    allCount = images.count;
    
    imgsArray = [NSMutableArray arrayWithArray:images];
    pageIndex = tag;
    
    //设置代理
    imageBack.delegate = self;
    _isHideBar = YES;
    imageBack.contentOffset = CGPointMake(kScreenWidth * (tag-1), 0);
    imageBack.pagingEnabled = YES;
    imageBack.contentSize = CGSizeMake(images.count * kScreenWidth, 0);
    imageBack.showsHorizontalScrollIndicator = NO;
    imageBack.showsVerticalScrollIndicator = NO;
    imageBack.backgroundColor = [UIColor blackColor];
    [self.view addSubview:imageBack];
    [images enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        UIImage *image = (UIImage *)obj;
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidth * idx, 0, kScreenWidth, kScreenHeight)];
        imgView.userInteractionEnabled = YES;
        imgView.backgroundColor = [UIColor blackColor];
        imgView.contentMode = UIViewContentModeScaleAspectFit;
        imgView.image = image;
        //加手势
        UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesClick:)];
        [imgView addGestureRecognizer:tapGes];
        [imageBack addSubview:imgView];
    }];
    
    //创建有按钮
    [self initRinghtBar];
}

- (void)initRinghtBar
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, 20, 20);
    [btn setImage:[UIImage imageNamed:@"healthy_icon_delete"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnClcik:) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
}

- (void)btnClcik:(UIButton *) btn
{
    UIActionSheet *actonSheet=[[UIActionSheet alloc] initWithTitle:@"要删除这张照片吗?" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"删除" otherButtonTitles:nil];
    
    [actonSheet showInView:self.view];
}

- (void)tapGesClick:(UITapGestureRecognizer *)tap
{
    _isHideBar = !_isHideBar;
    
    if (_isHideBar) {
        [UIView animateWithDuration:0.25 animations:^{
            CGRect frame = self.navigationController.navigationBar.frame;
            frame.origin.y = frame.origin.y + 64;
            self.navigationController.navigationBar.frame = frame;
        }];
    } else {
        [UIView animateWithDuration:0.25 animations:^{
            CGRect frame = self.navigationController.navigationBar.frame;
            frame.origin.y = frame.origin.y - 64;
            self.navigationController.navigationBar.frame = frame;
        }];
    }
    
}
#pragma mark - PictureViewDelegate
-(void)imageViewTap
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - UIScrollViewDelegate
- (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGPoint point = scrollView.contentOffset;
    NSInteger index = (NSInteger)(point.x / kScreenWidth + 1);
    pageIndex = index;
    self.navigationItem.title = [NSString stringWithFormat:@"%zd/%zd",index,allCount];
}

#pragma mark - actionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        [imgsArray removeObjectAtIndex:(pageIndex-1)];
        if (imgsArray.count == 0) {
            [self.navigationController popViewControllerAnimated:YES];
        }
        if (pageIndex-1==0) {
            [self setValueWithImages:imgsArray andImagView:1];
        } else {
            [self setValueWithImages:imgsArray andImagView:(pageIndex-1)];
        }
    } else {
        return;
    }
}
@end
